package com.cyx.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RedirectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();

        //底层方法实现重定向
        //resp.setHeader("Location",servletContext.getContextPath()+"/yzm");
        //resp.setStatus(302);
        //注意，不要写死项目的根路径,应该使用getContextPath
        resp.sendRedirect(servletContext.getContextPath()+"/yzm");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
